package com.mall.service.authority.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.model.domain.UmsResourceCategory;
import com.mall.service.authority.mapper.ResourceCategoryMapper;
import com.mall.service.authority.service.ResourceCategoryService;
import org.springframework.stereotype.Service;

/**
 * @Author Pan Rundong
 * @Date 2023-03-11 19:14
 * @Desc 后台资源Service实现类
 */
@Service
public class ResourceCategoryServiceImpl extends ServiceImpl<ResourceCategoryMapper, UmsResourceCategory> implements ResourceCategoryService {

    /**
     * 添加后台资源分类
     *      先检查是否存在同名模块，不存在则添加
     *
     * @param resourceCategory
     * @return void
     * @Author Pan Rundong
     */
    @Override
    public Boolean checkAndSave(UmsResourceCategory resourceCategory) {
        // 1.设置条件查询是否存在同名
        LambdaQueryWrapper<UmsResourceCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsResourceCategory::getName, resourceCategory.getName());
        UmsResourceCategory category = this.getOne(queryWrapper);

        // 2.判断，不存在则保存
        if (category == null) {
            this.save(resourceCategory);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 修改后台资源分类
     *      先查询表中是否存在该对象，存在则更新
     *
     * @param id
     * @param umsResourceCategory
     * @return void
     * @Author Pan Rundong
     */
    @Override
    public Boolean checkAndUpdate(Long id, UmsResourceCategory umsResourceCategory) {
        UmsResourceCategory resourceCategory = this.getById(id);
        if (resourceCategory != null) {
            this.updateById(umsResourceCategory);
            return true;
        } else {
            return false;
        }
    }
}
